// 2025/5/19
// 合并n个已排序的链表

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param lists ListNode类vector 
     * @return ListNode类
     */
    struct Compare
    {
        bool operator()(ListNode* n1, ListNode* n2)
        {
            return n1->val > n2->val;
        }
    };

    ListNode* mergeKLists(vector<ListNode*>& lists) {
        // write code here
        ListNode phead(-1001), *cur = &phead;
        priority_queue<ListNode*, vector<ListNode*>, Compare> pq;
        for(auto ptr : lists)
        {
            if(ptr) pq.push(ptr);
        }
        while(!pq.empty())
        {
            ListNode* tmp = pq.top();
            pq.pop();
            if(tmp->next)
                pq.push(tmp->next);
            tmp->next = nullptr;
            cur->next = tmp;
            cur = cur->next;
        }
        return phead.next;
    }
};